package com.mobile.liuyi_s.orderlunch.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.mobile.liuyi_s.orderlunch.MyApplication;
import com.mobile.liuyi_s.orderlunch.entity.Order;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by liuyi_s on 2017/8/3 10:26.
 * Email：liuyi_s@foxmail.com
 */


public class OrderUtils {
//    public void orderSubmit(final Context context,
//                               String userName, String site, int orderNum,
//                               int orderYear, int orderMonth, int orderDay) {
//        if (userName.length() > 0 && site.length() > 0 && orderNum > 0) {
//            Order order = new Order();
//            order.setOrderUser(userName);
//            order.setOrderSite(site);
//            order.setOrderNum(orderNum);
//            order.setOrderYear(orderYear);
//            order.setOrderMonth(orderMonth);
//            order.setOrderDay(orderDay);
//            order.save(new SaveListener<String>() {
//                @Override
//                public void done(String objectId, BmobException e) {
//                    if (e == null) {
//                        Toast.makeText(context, "订单提交成功", Toast.LENGTH_SHORT).show();
//                    } else {
//                        Toast.makeText(context, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
//                    }
//                }
//            });
//        }
//    }
    private Utils mUtils = new Utils();
    private static final String TAG = "OrderUtils";
    private static final int QUERY_ORDER_BY_SITE_50 = 350;
    private static final int QUERY_ORDER_BY_SITE_51 = 351;
    private static final int QUERY_ORDER_BY_SITE_52 = 352;
    private static final int QUERY_ORDER_BY_SITE_53 = 353;
    private static final int QUERY_ORDER_BY_SITE_54 = 354;


    private void getOrderNum(final String site) {
        Utils utils = new Utils();
        BmobQuery<Order> querySite = new BmobQuery<>();
        querySite.addWhereEqualTo("orderSite",site);
        BmobQuery<Order> queryYear = new BmobQuery<>();
        queryYear.addWhereEqualTo("orderYear", utils.getLocacYear());
        BmobQuery<Order> queryMonth = new BmobQuery<>();
        queryMonth.addWhereEqualTo("orderMonth", utils.getLocacMonth());
        BmobQuery<Order> queryDay = new BmobQuery<>();
        queryDay.addWhereEqualTo("orderDay", utils.getLocacDay());

        ArrayList<BmobQuery<Order>> queries = new ArrayList<>();
        queries.add(querySite);
        queries.add(queryYear);
        queries.add(queryMonth);
        queries.add(queryDay);
        BmobQuery<Order> mainQuery = new BmobQuery<>();
        mainQuery.and(queries);
        mainQuery.findObjects(new FindListener<Order>() {
            @Override
            public void done(List<Order> list, BmobException e) {
                if (e == null) {
                    Log.d(TAG, "done: list大小："+list.size());
                    if (list.size() > 0) {
                        Message message = mHandler.obtainMessage();
                        switch (site) {
                            case "50":
                                Log.d(TAG, "done: 50: "+list.size());
                                message.what = QUERY_ORDER_BY_SITE_50;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                break;
                            case "51":
                                Log.d(TAG, "done: 51: "+list.size());
                                message.what = QUERY_ORDER_BY_SITE_51;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                break;
                            case "52":
                                Log.d(TAG, "done: 52: "+list.size());
                                message.what = QUERY_ORDER_BY_SITE_52;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                break;
                            case "53":
                                Log.d(TAG, "done: 53: "+list.size());
                                message.what = QUERY_ORDER_BY_SITE_53;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                break;
                            case "54":
                                Log.d(TAG, "done: 54: "+list.size());
                                message.what = QUERY_ORDER_BY_SITE_54;
                                message.obj = list;
                                mHandler.sendMessage(message);
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        });
    }

    private Handler mHandler = new Handler() {
        int orderNum50 = 0;
        int orderNum51 = 0;
        int orderNum52 = 0;
        int orderNum53 = 0;
        int orderNum54 = 0;
        @Override
        public void handleMessage(Message msg) {
//            int listSize = ((List<Order>) msg.obj).size();
//            Log.d(TAG, "handleMessage: orderNumList：" + listSize);
            List<Order> orderList = (List<Order>) msg.obj;
            switch (msg.what) {
                case QUERY_ORDER_BY_SITE_50:
                    orderNum50 = mUtils.getOrderNumInList(orderList);
//                    orderNum50 = listSize;
                    Log.d(TAG, "handleMessage: orderNumList50：" + orderNum50);

                    break;
                case QUERY_ORDER_BY_SITE_51:
//                    orderNum51 = listSize;
                    orderNum51 = mUtils.getOrderNumInList(orderList);
                    Log.d(TAG, "handleMessage: orderNumList51：" + orderNum51);
                    break;
                case QUERY_ORDER_BY_SITE_52:
//                    orderNum52 = listSize;
                    orderNum52 = mUtils.getOrderNumInList(orderList);
                    Log.d(TAG, "handleMessage: orderNumList52：" + orderNum52);
                    break;
                case QUERY_ORDER_BY_SITE_53:
//                    orderNum53 = listSize;
                    orderNum53 = mUtils.getOrderNumInList(orderList);
                    Log.d(TAG, "handleMessage: orderNumList53：" + orderNum53);
                    break;
                case QUERY_ORDER_BY_SITE_54:
//                    orderNum54 = listSize;
                    orderNum54 = mUtils.getOrderNumInList(orderList);
                    Log.d(TAG, "handleMessage: orderNumList54：" + orderNum54);
                    break;
                default:
                    break;
            }
        }
    };
}
